﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Security;


namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// </summary>
    public partial class LoginView : UserControl, INavigationAware
    {
        private LoginViewModel Model;
        private readonly IUnityContainer _container;
        private readonly ISessionService _sessionService;

        public LoginView(IUnityContainer container, ISessionService sessionService)
        {
            InitializeComponent();

            _container = container;
            _sessionService = sessionService;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            Model = new LoginViewModel();
            Model.Session = new Session();
            Model.Session.User = new UserModel();

            this.DataContext = Model;
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            var session = _sessionService.Get(Model.Session.User.Login, Model.Session.User.Password);

            if (session == null || session.User == null)
            {
                Model.Message = "User was not located";
            }
            else
            {
                // regiseter user session
                _container.RegisterInstance<ISession>(session);
                Model.Message = "Logged in successfully.";
            }
        }
    }
}
